//
//  HRAnswerBannerViewController.m
//  Hear
//
//  Created by mac on 2017/12/29.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRAnswerBannerViewController.h"
#import "HRRecordAudioViewModel.h"

@interface HRAnswerBannerViewController (){
    NSTimer *myTimer;
    NSInteger timeCount;
    int number;
}
@property (nonatomic, strong)UIImageView *logoImageView;
@property(nonatomic, strong)UILabel *nameLabel;
@property(nonatomic, strong)UILabel *timeLabel;
@property(nonatomic, strong)EaseChatToolbar *chatToolbar;
@property(nonatomic, strong)UIButton *playButton;
@property(nonatomic, strong)UIImageView *showImageView;
@property(nonatomic, strong)HRRecordAudioViewModel *audioViewModel;

@end

@implementation HRAnswerBannerViewController
#pragma mark - Get and Set
- (HRRecordAudioViewModel *)audioViewModel{
    if (_audioViewModel == nil) {
        _audioViewModel = [[HRRecordAudioViewModel alloc] init];
        WeakSelf
        _audioViewModel.addEnditBlock = ^{
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.playButton.selected = YES;
                weakSelf.timeLabel.text = [NSString stringWithFormat:@"%dS",(int)weakSelf.audioViewModel.audioLength];
            });
        };
    }
    return _audioViewModel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"读广告";
    
    [self.audioViewModel checkRecord];

    
    [self setupView];
    // Do any additional setup after loading the view.
}

- (void)setupView{
    UIImageView *backImageView = [[UIImageView alloc] init];
    backImageView.frame = self.view.bounds;
    backImageView.image = [UIImage imageNamed:@"banner_bg"];
    [self.view addSubview:backImageView];
    backImageView.userInteractionEnabled = YES;
    
    UIButton *senderButton = [UIButton buttonWithType:0];
    senderButton.size = CGSizeMake(40, 30);
    [senderButton setTitle:@"提交" forState:0];
    [senderButton setTitleColor:[UIColor whiteColor] forState:0];
    senderButton.titleLabel.font = CustomUIFont(17);
    [senderButton addTarget:self action:@selector(senderButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:senderButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    UIImageView *logoImageView = [UIImageView new];
    logoImageView.backgroundColor = [UIColor lightGrayColor];
    logoImageView.image = [UIImage imageNamed:@""];
    logoImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:(self.logoImageView=logoImageView)];
    logoImageView.frame = CGRectMake(self.view.centerX-40, 100, 40, 40);
    
    
    
    self.nameLabel = [UILabel new];
    self.nameLabel.text = @"呵呵呵呵呵";
    self.nameLabel.font = CustomUIFont(14);
    self.nameLabel.textColor = [UIColor whiteColor];
    [self.view  addSubview:self.nameLabel];
    self.nameLabel.frame = CGRectMake(logoImageView.right, logoImageView.top, kFullWidth/2, logoImageView.height);
    
    
    UILabel *conentLabel = [UILabel new];
    conentLabel.text = @"打开断电看萨克打算考间房间侃大山大厦附近空发生看见的飞洒接口发动机卡死房价都卡的房间看";
    conentLabel.font = CustomUIFont(14);
    conentLabel.textColor = [UIColor whiteColor];
    [self.view  addSubview:conentLabel];
    conentLabel.numberOfLines = 0;
    conentLabel.frame = CGRectMake(20, self.nameLabel.bottom+20, kFullWidth-20*2, 40);
    
    
    
    UIImageView *showImageView = [UIImageView new];
    showImageView.image = [UIImage imageNamed:@"banner_外环"];
    [self.view addSubview:(self.showImageView=showImageView)];
    showImageView.frame = CGRectMake((kFullWidth-180)/2, self.view.centerY-30, 180, 180);
    showImageView.userInteractionEnabled = YES;
    
    UILabel *timeLabel = [UILabel new];
    timeLabel.font = CustomUIFont(19);
    timeLabel.textColor = [UIColor whiteColor];
    [self.view  addSubview:(self.timeLabel=timeLabel)];
    timeLabel.textAlignment = 1;
    timeLabel.frame = CGRectMake((kFullWidth-80)/2, showImageView.top, 80, 40);
    timeLabel.text = @"0S";
    
    UIButton *playButton = [UIButton buttonWithType:0];
    [playButton addTarget:self action:@selector(playButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [playButton setImage:[UIImage imageNamed:@"banner_播放键"] forState:0];
    [playButton setImage:[UIImage imageNamed:@"banner_暂停键"] forState:UIControlStateSelected];
    [showImageView addSubview:(self.playButton=playButton)];
    playButton.frame = CGRectMake((showImageView.width-45)/2, (showImageView.height-45)/2, 45, 45);
    
    
    
    WeakSelf
    CGFloat chatbarHeight = [EaseChatToolbar defaultHeight];
    self.chatToolbar = [[EaseChatToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - chatbarHeight, self.view.frame.size.width, chatbarHeight) type:HRChatToolbarTypeRecord];
    [self.view addSubview:self.chatToolbar];
    self.chatToolbar.backgroundColor = [UIColor clearColor];
    self.chatToolbar.toolbarView.backgroundColor = [UIColor clearColor];
    
    self.chatToolbar.stopRecordingBlock = ^() {
        [weakSelf.audioViewModel recorlAudio];
        [myTimer invalidate];
        myTimer = nil;
        timeCount = 0;
        
        NSString *tempstr = [self.timeLabel.text stringByReplacingOccurrencesOfString:@"S" withString:@""];
        weakSelf.chatToolbar.videotime = [tempstr intValue];
        weakSelf.chatToolbar.videoenter = @"0";
    };
    
    self.chatToolbar.starRecordingBlock = ^{
        myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
        [weakSelf.audioViewModel recorlAudio];
        
        weakSelf.chatToolbar.waterWaveView.recorder = weakSelf.audioViewModel.audioRecorder;
        
    };
    self.chatToolbar.againButtonBlock = ^{
        [weakSelf.audioViewModel recorlAudio];
    };
    
}

- (void)timeChange{
    timeCount ++;
    self.timeLabel.text = [NSString stringWithFormat:@"%dS",timeCount];
}

- (void)playButtonClick:(UIButton *)button{
    if (!self.audioViewModel.mp3Path.length){
        [MBProgressHUD showMessage:@"请先录制一个音频" finishBlock:nil];
        return;
    }
    button.selected = !button.isSelected;
    if (button.isSelected){
        [self.audioViewModel.audioPlayer play];
    }else{
        [self.audioViewModel.audioPlayer stop];
    }
}


- (void)playMusic:(NSString *)playPath{
    WeakSelf
    [[EMCDDeviceManager sharedInstance] asyncPlayingWithPath:playPath completion:^(NSError *error) {
        if (!error){
            weakSelf.playButton.selected = !weakSelf.playButton.isSelected;
        }
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    
    //设置导航栏背景图片为一个空的image，这样就透明了
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
    //去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}

- (void)viewWillDisappear:(BOOL)animated{
    
    //    如果不想让其他页面的导航栏变为透明 需要重置
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
    
    [myTimer invalidate];
    myTimer = nil;
    
    [self.audioViewModel.audioPlayer stop];
    [self.chatToolbar.waterWaveView stopAnimation];
    [[EMCDDeviceManager sharedInstance] cancelCurrentRecording];
}

-(void)dealloc{
    [self.chatToolbar.waterWaveView stopAnimation];
    [[EMCDDeviceManager sharedInstance] cancelCurrentRecording];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
